<!-- 高倍奖池管理 -->
<template>
  <SearchTable ref="SearchTableRef" :query="data.query" :list="data.list" :total="data.total" :showSet="false" @changeSize="changeSize"
    @changePage="changePage" @search="handleClick(data.nameType)" @reset="onHideDetails(true)">
    <template #other>
      <el-tabs v-model="data.nameType" type="card" class="demo-tabs" @tab-change="handleClick"
        style="user-select: none;">
        <el-tab-pane v-for="item in data.tabList" :label="item.label" :name="item.value" />
      </el-tabs>
    </template>
    <template #form>
      <Operator ref="OperatorRef" @query="queryItem" />
      <GameItem ref="GameItemRef" @query="queryGame" />
      <el-form-item label="" v-if="data.nameType == 1 ? true : false">
        <el-select class="selectWidth120" v-model="data.query.search_type" placeholder="请选择">
          <el-option v-for="item in data.conditionList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-input class="width200" v-model="data.query.search_value" placeholder="请输入" clearable
          onKeyUp="value=value.replace(/[\W]/g,'')" />
      </el-form-item>
      <el-form-item label="牌局编号：" v-if="data.nameType == 1 ? true : false">
        <el-input type="number" v-model="data.query.parent_id" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="返奖倍数：" v-if="data.nameType == 1 ? true : false">
        <el-input type="number" v-model="data.query.multiple" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="日期时间：" v-if="data.nameType == 1 ? true : false">
        <el-date-picker v-model="data.query.date" type="daterange" range-separator="-" start-placeholder="请选择开始时间"
          end-placeholder="请选择结束时间" value-format="YYYY-MM-DD" :clearable="false" :disabled-date="indexStore.disabledDateTime"/>
      </el-form-item>
    </template>
    <template #button>
      <el-button type="primary" @click="getPrizeWhitelist" v-if="data.nameType == 2 ? true : false">爆池白名单</el-button>
      <!-- <el-button type="warning" @click="data.isShowBatchControl = true"
        v-if="data.nameType == 2 ? true : false">奖池批量开关</el-button> -->
    </template>
    <template #table>
      <template v-for="(item, index) in data.nameType == 1 ? data.fields : data.nameType == 2 ? data.fieldsConfig : data.fieldsRecords"
        :key="index">
        <el-table-column v-if="item.visible" :prop="item.prop" :label="item.label" :width="item.width"
          show-overflow-tooltip>
          <template v-if="item.prop == 'account_id'" #default="scope">
            <span class="item_check" @click="checkUser(scope.row.account_id, 1)">
              {{ scope.row.account_id }}
            </span>
          </template>
          <template v-if="item.prop == 'account'" #default="scope">
            <span class="item_details" @click="checkUser(scope.row.account, 3), data.checkedItem = scope.row">
              {{ scope.row.account }}
            </span>
          </template>
          <template v-if="item.prop == 'agent_id'" #default="scope">
            {{ selectToLabel(indexStore.agentArray, scope.row.agent_id) }}
          </template>
          <template v-if="item.prop == 'game_id'" #default="scope">
            <div v-if="scope.row.game_id">
              <GameHeaderInfo :game_id="scope.row.game_id" />
            </div>
            <span v-else> - </span>
          </template>
          <template v-if="item.prop == 'game_type'" #default="scope">
            <el-tag>{{ selectToLabel(GAMETYPE, scope.row.game_type) }}</el-tag>
          </template>
          <template v-if="item.prop == 'game_mini'" #default="scope">
            <el-tag>{{ scope.row.small_game_type ? selectToLabel(data.gameTypeList, scope.row.small_game_type):'-'}}</el-tag>
          </template>
          <template v-if="item.prop == 'winningLosing'" #default="scope">
            <span :class="colorClass(BigWan(BigMinus(scope.row.win, scope.row.bet)))">{{
              BigWan(BigMinus(scope.row.win, scope.row.bet)) }}</span>
          </template>
          <template v-if="item.prop == 'prize_pool_map'" #default="scope">
            <span v-for="(second, two) in scope.row.prize_pool_map" :key="two">
              奖池序号：<span class="colorRed">{{ second.serial_num }}</span>,
              奖池状态：<span class="marginR10">
                <el-tag :type="second.status ? 'primary' : 'warning'">{{ selectToLabel(DEMOPLAY, second.status)
                  }}</el-tag>
              </span>,
              <span v-for="(third, three) in second.poolList" :key="three">
                <span class="colorRed">{{ third.label }}</span> 剩余额度：<span :class="colorClass(third.value)">{{
                  BigWan(third.value) }};</span>
              </span>
            </span>
          </template>
          <template v-if="item.prop == 'bet'" #default="scope">
            <span class="colorText">{{ BigWan(scope.row.bet) }}</span>
          </template>
          <template v-if="item.prop == 'multiple'" #default="scope">
            <span>{{ BigWan(scope.row.multiple) }}</span>
          </template>
          <template v-if="item.prop == 'after_pool'" #default="scope">
            <span class="colorText">{{ BigWan(scope.row.after_pool) }}</span>
          </template>
          <template v-if="item.prop == 'source'" #default="scope">
            <el-tag>{{ selectToLabel(data.sourceList, scope.row.source) }}</el-tag>
          </template>
          <template v-if="item.prop == 'save_time_stamp'" #default="scope">
            {{ setTimestampTime(scope.row.save_time_stamp) }}
          </template>
          <template v-if="item.prop == 'control_type'" #default="scope">
            <el-tag>{{ selectToLabel(data.behaviorList, scope.row.control_type) }}</el-tag>
          </template>
        </el-table-column>
      </template>
      <el-table-column fixed="right" label="详情" width="90" align="center" v-if="data.nameType == 1 ? true : false">
        <template #default="scope">
          <el-button type="primary" v-permiss="'/system/destroyManager'" size="small" @click="checkDetails(scope.row)">
            详 情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="设置" width="240" align="center" v-if="data.nameType == 2 ? true : false">
        <template #default="scope">
          <el-button type="danger" v-permiss="'/system/destroyManager'" size="small"
            @click="prizePoolControl(scope.row, 'control')">
            控制奖池
          </el-button>
          <el-button type="primary" v-permiss="'/system/destroyManager'" size="small"
            @click="prizePoolControl(scope.row, 'check')">
            查 看
          </el-button>
          <el-button type="warning" v-permiss="'/system/destroyManager'" size="small"
            @click="prizePoolControl(scope.row, 'details')">
            编 辑
          </el-button>
        </template>
      </el-table-column>
    </template>
  </SearchTable>

  <!-- 奖池信息 -->
  <el-dialog v-model="data.isShowMultipleControl" title="奖池信息" :close-on-click-modal="false" :destroy-on-close="true"
    :close-on-press-escape="false" width="55%" top="10vh" @close="onHideDetails(false)">
    <div v-loading="indexStore.moduleLoading" class="dialogMaxHeight">
      <div class="flex marginB20">
        【<span class="colorRed">{{ data.checkedItem.agent_id }}</span>】{{ selectToLabel(indexStore.agentArray,
          data.checkedItem.agent_id) }}
        <GameHeaderInfo class="marginL20" :game_id="data.checkedItem.game_id" />
      </div>
      <form-item title="大倍数奖池入池比例" :pass="true" value="a_slots_poolproportion">
        <el-input-number v-model="data.poolproportion" :min="0" :max="100" :precision="2" placeholder="大倍数奖池入池比例"
          clearable controls-position="right" />%
      </form-item>
      <form-item title="爆奖倍数控制" value="a_slots_multiplecontrol">
        <div class="flexWrap margin5" v-for="(item, index) in data.multipleControl" :key="index">
          档位:
          <el-input-number v-model="item.position" :min="-1" :max="99999999999999999999" :precision="0"
            class="imWidth80px marginR5" placeholder="档位" clearable :controls="false" />
          倍数下限:
          <el-input-number v-model="item.min" :min="-99999999999999999999" :max="99999999999999999999" :precision="0"
            class="width120 marginR5" placeholder="倍数下限" clearable :controls="false" />
          倍数上限:
          <el-input-number v-model="item.max" :min="0" :max="item.multiple" :precision="0" class="width120 marginR5"
            placeholder="倍数上限" clearable :controls="false" />
          开始判定倍数:
          <el-input-number v-model="item.multiple" :min="0" :max="99999999999999999999" :precision="0"
            class="width120 marginR5" placeholder="倍数上限" clearable :controls="false" />
        </div>
      </form-item>
      <form-item title="大倍数奖池控制" value="a_slots_poolproportion">
        <div class="flexWrap margin5" v-for="(item, index) in data.prizeControl" :key="index">
          奖池:
          <el-input-number v-model="item.position" :min="-1" :max="99999999999999999999" :precision="0"
            class="imWidth80px marginR5" placeholder="奖池序号" clearable :controls="false" />
          权重:
          <el-input-number v-model="item.weight" :min="0" :max="99999999999999999999" :precision="0"
            class="width120 marginR10" placeholder="权重" clearable :controls="false" />
          爆池档位下限:
          <el-input-number v-model="item.min" :min="-99999999999999999999" :max="99999999999999999999" :precision="0"
            class="width120 marginR10 " placeholder="爆池档位下限" clearable :controls="false" />
          爆池档位上限:
          <el-input-number v-model="item.max" :min="0" :max="99999999999999999999" :precision="0"
            class="width120 marginR10 marginT10" placeholder="爆池档位上限" clearable :controls="false" />
          <div class="marginT10 marginR10">
            爆池概率:
            <el-input-number v-model="item.chance" :min="0" :max="10000" :precision="2" class="width120 marginT10"
              placeholder="爆池档位上限" clearable :controls="false" />%
          </div>
          <div class="marginT10">
            状态开关控制：
            <el-radio-group class="marginT10" v-model="item.status" size="large" :disabled="data.isDisabledUser">
              <el-radio-button label="关闭" :value="0" />
              <el-radio-button label="开启" :value="1" />
            </el-radio-group>
          </div>
        </div>
      </form-item>
      <form-item title="奖池冷却" value="a_slots_cool">
        <div class="flexWrap margin5" v-for="(item, index) in data.slotsCool" :key="index">
          个人冷却:
          <el-input-number v-model="item.people" :min="0" :max="99999999999999999999" :precision="0"
            class="width120 marginR15" placeholder="个人冷却" clearable :controls="false" />
          游戏冷却:
          <el-input-number v-model="item.game" :min="0" :max="99999999999999999999" :precision="0" class="width120"
            placeholder="游戏冷却" clearable :controls="false" />
        </div>
      </form-item>
    </div>
    <template #footer v-if="!data.isDisabledUser">
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" v-loading="indexStore.moduleLoading" @click="editPrizePoolControl">确 定</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 奖池控制 -->
  <el-dialog v-model="data.isShowPrizeControl" title="奖池控制" :close-on-click-modal="false" :close-on-press-escape="false"
    :destroy-on-close="true" width="50%" top="20vh" @close="onHideDetails(false)">
    <div v-loading="indexStore.moduleLoading" class="dialogMaxHeight">
      <div class="flex marginB20">
        【<span class="colorRed">{{ data.checkedItem.agent_id }}</span>】{{ selectToLabel(indexStore.agentArray,
          data.checkedItem.agent_id) }}
        <GameHeaderInfo class="marginL20" :game_id="data.checkedItem.game_id" />
      </div>
      <p class="tipsTextRed">* 负数是扣除，正数是增加</p>
      <div class="border-prize" v-for="(item, index) in data.prizeControl" :key="index">
        奖池<span class="colorRed marginL2">{{ item.position }}</span>
        <span class="marginR10 marginL10">权重：{{ item.weight }}</span>
        <span class="marginR10">爆池概率：{{ item.chance }}%</span>
        <div class="marginL2 marginT5 flexWrap">
          <span class="marginR10" v-for="(second, two) in item.pool" :key="two">
            <span class="colorRed">{{ second.label }}</span> 剩余额度：<span :class="colorClass(second.value)">{{
              BigWan(second.value) }};</span>
            额度控制： <el-input-number :min="-9999999999999999" :max="9999999999999999" :precision="4"
              v-model="second.amount" placeholder="请输入额度" clearable :controls="false" />
          </span>
        </div>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" v-loading="indexStore.moduleLoading" @click="editPrizeControl">确 定</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 奖池批量控制 -->
  <el-dialog v-model="data.isShowBatchControl" title="奖池批量控制" :close-on-click-modal="false"
    :close-on-press-escape="false" width="50%" top="24vh" @close="onHideDetails(false)">
    <div class="dialogMaxHeight">
      <BatchControl ref="BatchControlRef" :gameType="1">
        <template #footer>
          <form-item title="奖池状态" value="status">
            <el-radio-group v-model="data.batchDetails.status" size="large">
              <el-radio-button label="开启奖池" :value="1" />
              <el-radio-button label="关闭奖池" :value="0" />
            </el-radio-group>
          </form-item>
        </template>
      </BatchControl>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" v-loading="indexStore.moduleLoading" @click="batchPrizePoolControl">确 定</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 爆池白名单 -->
  <el-dialog v-model="data.isShowWhiteDetails" title="爆池白名单" :close-on-click-modal="false" :destroy-on-close="true"
    :close-on-press-escape="false" width="55%" top="8vh" @close="data.isShowWhiteDetails = false">
    <div v-loading="indexStore.moduleLoading" class="dialogMaxMultipleButton">
      <SearchTable :query="data.queryDetails" :list="data.listDetails" :total="data.totalDetails" :showSet="false"
        @changeSize="changeWhiteDetailsSize" @changePage="changeWhiteDetailsPage" @search="getPrizeWhitelist"
        @reset="resetDetails">
        <template #form>
          <Operator ref="OperatorWhiteRef" @query="queryItemDetails" />
        </template>
        <template #button>
          <el-button type="success" @click="getPlayerList">新增玩家</el-button>
        </template>
        <template #table>
          <template v-for="(item, index) in data.fieldsDetails" :key="index">
            <el-table-column v-if="item.visible" :prop="item.prop" :label="item.label" :width="item.width">
              <template v-if="item.prop == 'agent_id'" #default="scope">
                {{ selectToLabel(indexStore.agentArray, scope.row.agent_id) }}
              </template>
              <template v-if="item.prop == 'save_time_stamp'" #default="scope">
                {{ setTimestampTime(scope.row.save_time_stamp) }}
              </template>
            </el-table-column>
          </template>
          <el-table-column fixed="right" label="操作" width="90" align="center">
            <template #default="scope">
              <el-button type="danger" v-permiss="'/system/destroyManager'" size="small"
                @click="delWhitelisUser(scope.row)">
                删 除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </SearchTable>
    </div>
  </el-dialog>

  <!-- 添加玩家 -->
  <el-dialog v-model="data.isShowPlayer" title="玩家爆池白名单" :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true"
    width="50%" top="9vh" @close="data.isShowPlayer = false" >
    <div v-loading="indexStore.moduleLoading" class="dialogMaxMultipleButton">
      <SearchTable :query="data.queryDetails" :list="data.listPlayer" :total="data.totalPlayer" :showSet="false"
        @changeSize="changePlayerSize" @changePage="changePlayerPage" @search="getPlayerList" @reset="resetDetails">
        <template #form>
          <el-form-item label="">
            <el-select class="selectWidth120" v-model="data.queryDetails.search_type" placeholder="请选择">
              <el-option v-for="item in data.conditionList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
            <el-input class="width200" v-model="data.queryDetails.search_value" placeholder="请输入" clearable
              onKeyUp="value=value.replace(/[\W]/g,'')" />
          </el-form-item>
        </template>
        <template #button></template>
        <template #table>
          <template v-for="(item, index) in data.fieldsPlayer" :key="index">
            <el-table-column v-if="item.visible" :prop="item.prop" :label="item.label" :width="item.width">
              <template v-if="item.prop == 'agent_id'" #default="scope">
                {{ selectToLabel(indexStore.agentArray, scope.row.agent_id) }}
              </template>
              <template v-if="item.prop == 'today_game_income'" #default="scope">
                <span :class="colorClass(scope.row.today_game_income)">{{ BigWan(scope.row.today_game_income)
                  }}</span>
              </template>
              <template v-if="item.prop == 'total_game_income'" #default="scope">
                <span :class="colorClass(scope.row.total_game_income)">{{ BigWan(scope.row.total_game_income)
                  }}</span>
              </template>
            </el-table-column>
          </template>
          <el-table-column fixed="right" label="操作" width="90" align="center">
            <template #default="scope">
              <el-button type="success" v-permiss="'/system/destroyManager'" size="small"
                @click="addWhitelisUser(scope.row)">
                添 加
              </el-button>
            </template>
          </el-table-column>
        </template>
      </SearchTable>
    </div>
  </el-dialog>

  <!-- 变更表头弹框 -->
  <tableHeader :dialogVisible="data.dialogVisible" :list="data.fields" @cancel="data.dialogVisible = false"
    @resetData="resetData" />

  <!-- 游戏详情 -->
  <GameDetail :show="data.isShowDetails" :checkedItem="data.checkedItem" @onHideDetails="onHideDetails" />

  <!-- 玩家信息详情 -->
  <UserDetails :show="data.isUserDetails" @onHideDetails="onHideDetails" :checkedItem="data.checkedItem" />
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import Operator, { type QueryType } from '@/components/FormItem/Operator.vue'
import GameItem, { type GameType } from '@/components/FormItem/GameItem.vue'
import tableHeader from '@/components/tableHeader/tableHeader.vue'
import SearchTable from '@/components/SearchTable/SearchTable.vue'
import GameDetail from '@/views/player/components/game-details.vue'
import UserDetails from '@/views/player/components/user-details.vue'
import BatchControl from '@/components/FormItem/BatchControl.vue'
import GameHeaderInfo from '@/components/FormItem/GameHeaderInfo.vue'
import { GAMETYPE, DEMOPLAY } from '@/config/eunm'
import {
  throttle,
  selectToLabel,
  colorClass,
  setCommaToArr,
  setArrStr,
  setObjStr,
  setSemicolonToArr,
} from '@/utils/index'
import { BigHundred, BigWan, BigMinus, BigTimeWan, BigTimeHundred } from '@/utils/math'
import { setTimestampTime, setTimeValue, getDateDay } from '@/utils/time'
import { getPlayer } from '@/api/player'
import { getGame } from '@/api/game'
import {
  getExplosionList,
  addExplosionWhitelis,
  deleteExplosionWhitelis,
  getPrizeWhitelistList,
  getPoolPrizeWhite,
  getPrizeLimit,
  editPrize,
  editPrizeAmount,
  editBatchPool,
} from '@/api/config'

import { useStore } from '@/stores/index'
const indexStore = useStore()

const data = reactive({
  query: {
    page: 1,
    size: 50,
    date: [getDateDay(0),getDateDay(0)],
    agent_id: -1,
    game_type: 1,
    operated_id: -1,
    game_id: -1,
    platform: -1,
    nameType: 1,
    multiple: '',
    parent_id: '',
    search_type: 1,
    search_value: '',
  },
  list: [],
  total: 0,
  fields: [
    { label: '平台ID', prop: 'account_id', width: '150', visible: true },
    { label: '平台昵称', prop: 'account', width: '150', visible: true },
    { label: '商户', prop: 'agent_id', width: '150', visible: true },
    { label: '牌局编号', prop: 'parent_id_str', width: '200', visible: true },
    { label: '游戏名称', prop: 'game_id', width: '200', visible: true },
    { label: '游戏类型', prop: 'game_type', width: '100', visible: true },
    { label: '游戏玩法', prop: 'game_mini', width: '120', visible: true },
    { label: '玩家输赢', prop: 'winningLosing', width: '150', visible: true },
    { label: '投注额度', prop: 'bet', width: '100', visible: true },
    { label: '返奖倍数', prop: 'multiple', width: '100', visible: true },
    { label: '奖池序号', prop: 'pool_type', width: '100', visible: true },
    { label: '奖池剩余额度', prop: 'after_pool', width: '140', visible: true },
    { label: '来源', prop: 'source', width: '120', visible: true },
  ],
  fieldsConfig: [
    { label: '商户', prop: 'agent_id', width: '150', visible: true },
    { label: '游戏名称', prop: 'game_id', width: '240', visible: true },
    { label: '奖池信息', prop: 'prize_pool_map', width: '', visible: true },
  ],
  fieldsRecords: [
    { label: '操作账号', prop: 'username', width: '150', visible: true },
    { label: '时间', prop: 'save_time_stamp', width: '180', visible: true },
    { label: '商户', prop: 'agent_id', width: '150', visible: true },
    { label: '游戏', prop: 'game_id', width: '200', visible: true },
    { label: '行为', prop: 'control_type', width: '', visible: true },
    { label: '修改描述', prop: 'remark', width: '', visible: true },
  ],
  fieldsPlayer: [
    { label: '商户', prop: 'agent_id', width: '', visible: true },
    { label: '平台ID', prop: 'account3d_id', width: '', visible: true },
    { label: '平台昵称', prop: 'account3d', width: '', visible: true },
    { label: '今日输赢', prop: 'today_game_income', width: '', visible: true },
    { label: '历史输赢', prop: 'total_game_income', width: '', visible: true },
  ],
  fieldsDetails: [
    { label: '商户', prop: 'agent_id', width: '', visible: true },
    { label: '平台ID', prop: 'account_id', width: '', visible: true },
    { label: '平台昵称', prop: 'account', width: '', visible: true },
    { label: '添加时间', prop: 'save_time_stamp', width: '', visible: true },
  ],
  gameFields: [
    { label: 'ID', prop: 'uuid_str', width: '90', visible: true, tips: '此条游戏记录的ID' },
    { label: '创建时间', prop: 'save_time_stamp', width: '180', visible: true, tips: '此条游戏记录的生成时间，也为玩家当局游戏结算的时间' },
    { label: '牌局编号', prop: 'round_id_str', width: '195', visible: true, tips: '此条游戏记录的编号，唯一，对应客户端的展示用于比对查找' },
    { label: '平台ID', prop: 'account_id', width: '150', visible: true, tips: '玩家ID编号，唯一' },
    { label: '平台昵称', prop: 'account', width: '150', visible: true, tips: '玩家名称' },
    { label: '商户/地区', prop: 'agent_id', width: '200', visible: true, tips: '此玩家所属商户和地区' },
    { label: '游戏名称', prop: 'game_id', width: '200', visible: true, tips: '此条记录的产生来源，游戏的名称说明' },
    { label: '游戏类型', prop: 'game_type', width: '120', visible: true, tips: '此游戏的类型，区分为百人、MiniGame、老虎机' },
    { label: '游戏玩法', prop: 'game_mini', width: '120', visible: true, tips: '标记玩家在游戏过程中的玩法类型<br>“-":普通转动；<br> “免费旋转":玩家触发了游戏内的特殊玩法，如免费游戏等；<br> “连消":玩家在普通转动中中奖连续消除；<br> “重转":疯狂钱来也、森巴嘉年华游戏的特点玩法；' },
    { label: '玩家输赢', prop: 'win_lose', width: '150', visible: true, tips: '此局游戏玩家的输赢情况，正为玩家盈利，负为玩家亏损' },
    { label: '下注前', prop: 'enter_money', width: '180', visible: true, tips: '玩家在此局游戏下注前的持有余额数量' },
    { label: '结算后', prop: 'after_settlement_money', width: '180', visible: true, sortable: true, tips: '玩家在此局游戏下注、结算后的持有余额数量' },
    { label: '下注金额', prop: 'bet', width: '150', visible: true, sortable: true, tips: '玩家在此局游戏的下注额度' },
  ],
  queryDetails: {
    page: 1,
    size: 50,
    agent_id: -1,
    operated_id: -1,
    search_type: 1,
    search_value: '',
  },
  batchDetails: {
    operated_id: -1,
    agent_ids: [],
    game_ids: [],
    status: 1,
  },
  listDetails: [],
  totalDetails: 0,
  listPlayer: [],
  totalPlayer: 0,
  conditionList: [
    { label: '平台ID', value: 1 },
    { label: '平台昵称', value: 3 }
  ],
  sourceList: [
    { label: '白名单爆出', value: 1 },
    { label: '正常触发', value: 2 }
  ],
  behaviorList: [
    { label: '关闭奖池', value: 0 },
    { label: '打开奖池', value: 1 },
    { label: '修改奖池额度', value: 2 },
    { label: '新增奖池', value: 3 },
    { label: '添加爆池白名单', value: 4 },
    { label: '删除爆池白名单', value: 5 },
  ],
  gameTypeList: [
    { label: '连消', value: 1 },
    { label: '免费旋转', value: 2 },
    { label: '重转', value: 3 },
    { label: '高倍', value: 4 }
  ],
  gameDetails: [],
  exchange_rate: 0,
  isDisabledUser: false,
  dialogVisible: false,
  isShowDetails: false,
  isShowWhiteDetails: false,
  isShowPlayer: false,
  isShowPrizeControl: false,
  isShowMultipleControl: false,
  isShowBatchControl: false,
  isUserDetails: false,
  prizeControl: [] as any[],
  oldPrizeControl: [] as any[],
  multipleControl: [] as any[],
  poolproportion: 0,
  slotsCool: [] as any[],
  checkedItem: {} as any,
  nameType: 1,
  tabList: [
    { label: '爆池记录', value: 1 },
    { label: '配置管理', value: 2 },
    { label: '奖池控制记录', value: 3 },
  ],
})

// 商户类别 + 商户
const queryItem = (item: QueryType) => {
  data.query.agent_id = item.agent_id
  data.query.operated_id = item.operated_id
}
const queryItemDetails = (item: QueryType) => {
  data.queryDetails.agent_id = item.agent_id
  data.queryDetails.operated_id = item.operated_id
}
// 游戏品牌 + 游戏
const queryGame = (item: GameType) => {
  data.query.platform = item.platform
  data.query.game_id = item.game_id
}

const GameItemRef = ref<any>()
onMounted(async () => {
  handleClick(data.nameType)
  GameItemRef.value.getGameType(1)
})

const getSearchList = async () => {
  indexStore.setModuleLoading(true)
  data.query = setTimeValue(data.query)
  let query = JSON.parse(JSON.stringify(data.query))
  let res = await getExplosionList(query)
  data.list = res.data.list
  data.total = res.data.total
}
const getSearchGameList = async () => {
  indexStore.setModuleLoading(true)
  let query = JSON.parse(JSON.stringify(data.query));
  query.search_type = 1
  let res = await getGame(query)
  res.data.list.forEach((item: any) => {
    // 奖池信息
    item.prize_pool_map?.forEach((second: any) => {
      let keys = Object.keys(second.pool_num);
      second.poolList = []
      keys?.forEach((third: any) => {
        second.poolList.push({ label: selectToLabel(indexStore.serverNameList, third), value: second.pool_num[third] })
      })
    })
  })
  data.list = res.data.list
  data.total = res.data.total
}
// 获取奖池/白名单修改记录
const getPrizeWhiteRecordsList = async () => {
  indexStore.setModuleLoading(true)
  let query = JSON.parse(JSON.stringify(data.query))
  let res = await getPrizeWhitelistList(query)
  data.list = res.data.list
  data.total = res.data.total
}

// 选择游戏tab
const SearchTableRef = ref<any>()
const handleClick = async (value: number) => {
  data.nameType = value
  data.list = []
  data.total = 0
  data.query.page = 1
  data.query.size = 50
  SearchTableRef.value.initTableMaxHeight()
  indexStore.setPageLoading(true)
  if (data.nameType == 3) {
    getPrizeWhiteRecordsList()
  } else if (data.nameType == 2) {
    getSearchGameList()
  } else {
    getSearchList()
  }
}

// 爆池白名单
const getPrizeWhitelist = async () => {
  indexStore.setModuleLoading(true)
  let query = JSON.parse(JSON.stringify(data.queryDetails))
  let res = await getPoolPrizeWhite(query)
  data.listDetails = res.data.list
  data.totalDetails = res.data.total
  data.isShowWhiteDetails = true
}

// 删除玩家白名单
const delWhitelisUser = async (row: any) => {
  ElMessageBox.confirm(`是否删除【${row.account_id}】${row.account}玩家白名单`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    await deleteExplosionWhitelis({ id: row.id })
    ElMessage.success('删除成功')
    getPrizeWhitelist()
  }).catch(() => { })
}

// 获取玩家
const getPlayerList = async () => {
  let query = JSON.parse(JSON.stringify(data.queryDetails))
  query.agent_id = -1,
    query.operated_id = -1
  let res = await getPlayer(query)
  data.listPlayer = res.data.list
  data.totalPlayer = res.data.total
  data.isShowPlayer = true
}

// 新增玩家白名单
const addWhitelisUser = throttle(async (row: any) => {
  indexStore.setModuleLoading(true)
  await addExplosionWhitelis({
    user_id: row.id,
    agent_id: row.agent_id,
    account_id: row.account3d_id,
    account: row.account3d
  })
  ElMessage.success('新增成功')
  data.queryDetails.agent_id = -1
  data.queryDetails.operated_id = -1
  OperatorWhiteRef.value.onHideDetails()
  getPrizeWhitelist()
}, 2000)

// 奖池控制
const prizePoolControl = async (row: any, type: string) => {
  indexStore.setModuleLoading(true)
  Object.assign(data.checkedItem, row)
  let res = await getPrizeLimit({
    game_id: row.game_id,
    agent_id: row.agent_id,
  })
  data.prizeControl = setArrBigControl(res.data.game.a_slots_bigamountcontrol, res.data.pool) // 大倍数奖池控制
  data.oldPrizeControl = setArrBigControl(res.data.game.a_slots_bigamountcontrol, res.data.pool) // 大倍数奖池控制(用于编辑时候比对修改)
  data.multipleControl = setArrMultipleControl(res.data.game.a_slots_multiplecontrol) // 爆奖倍数控制
  data.poolproportion = BigHundred(res.data.game.a_slots_poolproportion) // 大倍数奖池入池比例(万分比)
  data.slotsCool = setArrCoolControl(res.data.game.a_slots_cool) // 奖池冷却
  if (type == 'control') {
    data.isShowPrizeControl = true
  } else {
    if (type == 'check') {
      data.isDisabledUser = true
    }
    data.isShowMultipleControl = true
  }
}
// 修改奖池信息
const editPrizePoolControl = throttle(async () => {
  indexStore.setModuleLoading(true)
  let serial = [] as number[], types = [] as number[];
  data.oldPrizeControl.forEach((item: any) => {
    data.prizeControl.forEach((second: any) => {
      if (item.position == second.position && item.status != second.status) {
        serial.push(item.position)
        types.push(second.status)
      }
    })
  })
  await editPrize({
    game_id: data.checkedItem.game_id,
    agent_id: data.checkedItem.agent_id,
    a_slots_bigamountcontrol: setStrBigControl(data.prizeControl),
    a_slots_multiplecontrol: setStrMultipleControl(data.multipleControl),
    a_slots_poolproportion: BigTimeHundred(data.poolproportion),
    a_slots_cool: setStrCoolControl(data.slotsCool),
    edit_types: types,
    serial_nums: serial,
  })
  ElMessage.success('修改成功')
  data.isShowMultipleControl = false
  handleClick(data.nameType)
},5000)

// 修改奖池控制
const editPrizeControl = throttle(async () => {
  indexStore.setModuleLoading(true)
  let amounts: any[] = [], serial: any[] = [];
  data.prizeControl.forEach((item: any) => {
    item.pool.forEach((second: any) => {
      amounts.push(BigTimeWan(second.amount))
      serial.push(item.position + '_' + second.remark)
    })
  })
  await editPrizeAmount({
    game_id: data.checkedItem.game_id,
    agent_id: data.checkedItem.agent_id,
    amounts: amounts,
    serial_num_server_id_arr: serial,
  })
  ElMessage.success('修改成功')
  prizePoolControl(data.checkedItem, 'control')
  handleClick(data.nameType)
},5000)
// 奖池批量控制
const batchPrizePoolControl = throttle(async () => {
  indexStore.setModuleLoading(true)
  let query = JSON.parse(JSON.stringify(data.batchDetails));
  Object.assign(query, BatchControlRef.value.SetBatchControl())
  if (!query.game_ids.length) {
    return false
  }
  if (!query.agent_ids.length) {
    return false
  }
  await editBatchPool(query)
  ElMessage.success('修改成功')
  handleClick(data.nameType)
},5000)

// 查看玩家信息
const checkUser = async (val: any, type: number) => {
  if (type == 1) {
    data.query.search_type = type
    data.query.search_value = val
    getSearchList()
  } else {
    data.isUserDetails = true
  }
}
// 详情
const checkDetails = (row: any) => {
  data.isShowDetails = true
  Object.assign(data.checkedItem, row)
}

// 爆奖倍数控制 
const setArrMultipleControl = (str: string) => {
  let arr = [], newArr: any[] = [];
  arr = setSemicolonToArr(str)
  arr?.forEach((item: any) => {
    newArr.push({
      position: Number(item[0]),
      multiple: Number(item[1]),
      min: Number(item[2]),
      max: Number(item[3]),
    })
  })
  return newArr
}
const setStrMultipleControl = (arr: any) => {
  let newArr: any[] = []
  arr?.forEach((item: any) => {
    newArr.push({
      position: item.position ? item.position : 0,
      multiple: item.multiple,
      min: item.min ? item.min : 0,
      max: item.max ? item.max : 0,
    })
  })
  return setArrStr(newArr)
}
// 大倍数奖池控制
const setArrBigControl = (str: string, obj: any) => {
  let arr = [], newArr: any[] = [], keys = Object.keys(obj);
  arr = setSemicolonToArr(str)
  // 奖池
  arr?.forEach((item: any) => {
    let poolNew: any[] = [];
    keys.forEach((key: any) => {
      // 奖池一致
      if (Number(item[0]) == Number(key)) {
        // 那些奖池
        let keyTwo = Object.keys(obj[key])
        keyTwo.forEach((second: any) => {
          poolNew.push({ label: selectToLabel(indexStore.serverNameList, second), value: obj[key][second], remark: second, amount: 0 })
        })
      }
    })
    newArr.push({
      position: Number(item[0]),
      weight: Number(item[1]),
      min: Number(item[2]),
      max: Number(item[3]),
      chance: BigHundred(item[4]),
      pool: poolNew,
      status: Number(item[5]),
    })
  })
  return newArr
}
const setStrBigControl = (arr: any) => {
  let newArr: any[] = []
  arr?.forEach((item: any) => {
    newArr.push({
      position: item.position ? item.position : 0,
      weight: item.weight ? item.weight : 0,
      min: item.min ? item.min : 0,
      max: item.max ? item.max : 0,
      chance: item.chance ? BigTimeHundred(item.chance) : 0,
      status: item.status,
    })
  })
  return setArrStr(newArr)
}
// 奖池冷却 
const setArrCoolControl = (str: string) => {
  let arr = [], newArr: any[] = [];
  arr = setCommaToArr(str)
  newArr = [{ people: Number(arr[0]), game: Number(arr[1]), }]
  return newArr
}
const setStrCoolControl = (arr: any) => {
  let newArr: any[] = []
  arr?.forEach((item: any) => {
    newArr.push({
      people: item.people ? item.people : 0,
      game: item.game ? item.game : 0,
    })
  })
  return setObjStr(newArr[0])
}
// 修改页数
const changeSize = (val: number) => {
  data.query.size = val
}
const changePage = (val: number) => {
  data.query.page = val
  if (data.nameType == 3) {
    getPrizeWhiteRecordsList()
  } else if (data.nameType == 2) {
    getSearchGameList()
  } else {
    getSearchList()
  }
}
const changeWhiteDetailsSize = (val: number) => {
  data.queryDetails.size = val
  getPrizeWhitelist()
}
const changeWhiteDetailsPage = (val: number) => {
  data.queryDetails.page = val
  getPrizeWhitelist()
}
const changePlayerSize = (val: number) => {
  data.queryDetails.size = val
  getPlayerList()
}
const changePlayerPage = (val: number) => {
  data.queryDetails.page = val
  getPlayerList()
}
// 重置表头
const resetData = (val: any) => {
  data.fields = val
}

// 重置
const OperatorRef = ref<any>()
const BatchControlRef = ref<any>()
const OperatorWhiteRef = ref<any>()
const onHideDetails = (show: boolean) => {
  data.checkedItem = {} as any
  data.dialogVisible = false
  data.isShowDetails = false
  data.isShowMultipleControl = false
  data.isShowPrizeControl = false
  data.isDisabledUser = false
  data.isUserDetails = false
  if (data.isShowPlayer) {
    return getPlayerList()
  } else {
    data.isShowPlayer = false
  }
  if (data.isShowWhiteDetails) {
    return getPrizeWhitelist()
  } else {
    data.isShowWhiteDetails = false
  }
  if (data.isShowBatchControl) {
    BatchControlRef.value.onHideDetails()
  }
  data.isShowBatchControl = false
  data.batchDetails = {
    operated_id: -1,
    agent_ids: [],
    game_ids: [],
    status: 1,
  }
  if (show) {
    data.query = {
      page: 1,
      size: 50,
      date: [getDateDay(0),getDateDay(0)],
      agent_id: -1,
      game_type: 1,
      operated_id: -1,
      game_id: -1,
      platform: -1,
      nameType: 1,
      multiple: '',
      parent_id: '',
      search_type: 1,
      search_value: '',
    }
    OperatorRef.value.onHideDetails()
    GameItemRef.value.onHideDetails(1)
    handleClick(data.nameType)
  }
}
const resetDetails = () => {
  if (data.isShowPlayer) {
    data.queryDetails.search_type = 1
    data.queryDetails.search_value = ''
    getPlayerList()
  }
  if (data.isShowWhiteDetails && !data.isShowPlayer) {
    data.queryDetails = {
      page: 1,
      size: 50,
      agent_id: -1,
      operated_id: -1,
      search_type: 1,
      search_value: '',
    }
    OperatorWhiteRef.value.onHideDetails()
    getPrizeWhitelist()
  }
}
</script>

<style lang="scss" scoped>
.prize {
  width: 98%;
  max-height: 300px;
  border: 1px solid #dddddd;
  padding: 5px 10px;
  border-radius: 5px;
  overflow: auto;
}

.border-prize {
  border: 1px solid #ededed;
  padding: 10px;
  border-radius: 5px;
  margin: 5px 0;
}
</style>